package com.caixin.application.user;

import com.caixin.application.user.map.UserMap;
import com.caixin.domain.user.agg.User;
import com.caixin.domain.user.repository.UserRepo;
import com.caixin.domain.user.service.UserDomainService;
import com.caixin.domain.user.specification.UserSpecification;
import com.caixin.domain.user.types.unique.UserId;
import com.caixin.service.user.UserCmdService;
import com.caixin.service.user.command.UserAddCmd;
import com.caixin.service.user.dto.UserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * 系统用户Cmd服务
 *
 * @author caixin
 * @version 1.0.0
 * @date 2021年12月11日 11:21
 */
@Service
@Slf4j
public class UserCmdServiceImpl implements UserCmdService {

    @Autowired
    private UserMap userMap;
    @Autowired
    private UserRepo userRepo;
    @Autowired
    private UserDomainService userDomainService;
    @Autowired
    private UserSpecification userSpecification;


    @Override
    public void addUser(UserAddCmd cmd) {

        if (userSpecification.checkCreateUser()) {
            log.info("=================检查完毕！======================");
        }
        //逻辑业务校验
        userDomainService.checkCreateUser(cmd);
        //模型转换
        User userAgg = userMap.userAddCmdFormat(cmd);
        //聚合校验
        userAgg.valid();
        //输出日志
        log.info(userAgg.toString());
        log.error("=================数据转换已完成！======================");
        //保存
        userRepo.save(userAgg);

    }

    /**
     * 用户详细
     *
     * @param id:
     * @return com.service.user.dto.UserDTO
     * @date 2021年12月07日 17:53
     */
    @Override
    public UserDTO findUserById(Long id) {
        //读取用户聚合信息
        User user = userRepo.find(new UserId(id));
        Assert.notNull(user, "未找到内容！");
        //转换
        return userMap.aggToUserDTO(user);

    }


}
